package com.newangels.util;

import jade.core.AID;
import jade.lang.acl.ACLMessage;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.logging.Logger;

/**
 * ACLMessage建造
 *
 * @author: TangLiang
 * @date: 2020/4/21 8:35
 * @since 1.3
 */
public class MessageBuilder {
    private static Logger logger = Logger.getLogger(MessageBuilder.class.getName());
    private final ACLMessage message;

    private MessageBuilder(int performative) {
        this.message = new ACLMessage(performative);
    }

    public static MessageBuilder ACLMessageBuilder(int performative) {
        return new MessageBuilder(performative);
    }

    public MessageBuilder to(AID... recievers) {
        for (AID reciever : recievers) {
            message.addReceiver(reciever);
        }
        return this;
    }

    public MessageBuilder toLocal(String... otherAgentNames) {
        for (String agentName : otherAgentNames) {
            AID address = new AID(agentName, AID.ISLOCALNAME);
            message.addReceiver(address);
        }
        return this;
    }

    public MessageBuilder Performative(int perf) {
        message.setPerformative(perf);
        return this;
    }

    public MessageBuilder withContent(String content) {
        message.setContent(content);
        return this;
    }

    public <T> MessageBuilder withContent(T t) {
        message.setContent(t.toString());
        return this;
    }

    public <T extends Serializable> MessageBuilder withContentObject(T... ts) {
        for (T t : ts) {
            try {
                message.setContentObject(t);
            } catch (IOException e) {
                logger.severe(t.getClass().getName() + "序列化异常,发送失败" + e.getMessage());
                e.printStackTrace();
            }
        }
        return this;
    }

    public MessageBuilder protocol(String protocol) {
        message.setProtocol(protocol);
        return this;
    }

    public MessageBuilder ontology(String ontology) {
        message.setOntology(ontology);
        return this;
    }

    public MessageBuilder replyWith(String replyWith) {
        message.setReplyWith(replyWith);
        return this;
    }

    public MessageBuilder inReplyTo(String inReplyTo) {
        message.setInReplyTo(inReplyTo);
        return this;
    }

    public MessageBuilder encoding(String Encoding) {
        message.setEncoding(Encoding);
        return this;
    }

    public MessageBuilder conversationId(String conversationId) {
        message.setConversationId(conversationId);
        return this;
    }

    public MessageBuilder replyTo(AID dest) {
        message.addReplyTo(dest);
        return this;
    }

    public MessageBuilder language(String language) {
        message.setLanguage(language);
        return this;
    }

    public MessageBuilder replyByDate(Date date) {
        message.setReplyByDate(date);
        return this;
    }

    public MessageBuilder byteSequenceContent(byte[] byteSequenceContent) {
        message.setByteSequenceContent(byteSequenceContent);
        return this;
    }

    public ACLMessage build() {
        return message;
    }
}
